package com.css.fxfzypg.modules.rating.task.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.rating.task.model.dto.ApplyYpgTaskDto;
import com.css.fxfzypg.modules.rating.task.model.dto.NewKpTaskDto;
import com.css.fxfzypg.modules.rating.task.service.KpRatingTaskNewService;
import com.css.fxfzypg.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 新建考评任务接口
 */
@RestController
@RequestMapping("kpRatingTask/new")
@Slf4j
public class KpRatingTaskNewController {
    @Autowired
    KpRatingTaskNewService kpRatingTaskNewService;

    /**
     * 获取用户上次未保存的任务
     *
     * @return
     */
    @GetMapping("getLatestUnsaveTask")
    public RestResponse<NewKpTaskDto> getLatestUnsaveTask() {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("获取用户上次未保存的任务，用户id:{}", currUid);
            return RestResponse.succeed(kpRatingTaskNewService.getLatestUnsaveTask(currUid));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 应用关联任务
     *
     * @return
     */
    @PostMapping("applyGlTask")
    public RestResponse<ApplyYpgTaskDto> applyGlTask(@RequestBody ApplyYpgTaskDto dto) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("应用关联任务，用户id:{},参数:{}", currUid, JSONObject.toJSONString(dto));
            kpRatingTaskNewService.applyGlTask(dto, currUid);
            return RestResponse.succeed(dto);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 删除关联任务
     */
    @GetMapping("deleteGlTask")
    public RestResponse<String> deleteGlTask(@RequestParam String id) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("删除关联任务，用户id:{},参数:{}", currUid, id);
            kpRatingTaskNewService.deleteGlTask(id, currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 清空考评任务
     *
     * @return
     */
    @GetMapping("cleanKpTask")
    public RestResponse<String> cleanKpTask() {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("清空任务，用户id:{}", currUid);
            kpRatingTaskNewService.cleanKpTask(currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

//    /**
//     * 保存考评任务
//     *
//     * @return
//     */
//    @PostMapping("saveKpTask")
//    public RestResponse<String> newKpTaskSaveKpTask(@RequestBody NewKpTaskDto dto) {
//        try {
//            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
//            if (currUid == null) {
//                return RestResponse.fail("请先登录");
//            }
//            log.debug("清空任务，用户id:{}", currUid);
//            kpRatingTaskNewService.newKpTaskSaveKpTask(dto, currUid);
//            return RestResponse.succeed();
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//            return RestResponse.fail(e.getMessage());
//        }
//    }

    /**
     * 保存考评任务
     *
     * @return
     */
    @GetMapping("saveKpTask")
    public RestResponse<String> saveKpTask() {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            log.debug("保存考评任务，用户id:{}", currUid);
            kpRatingTaskNewService.saveKpTask(currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }
}
